package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.mapper.OutPersonMapper;
import com.bayihouse.model.entity.OutPerson;
import com.bayihouse.model.entity.Room;
import com.bayihouse.model.request.outperson.OutPersonQueryRequest;
import com.bayihouse.model.vo.OutPersonVO;
import com.bayihouse.model.vo.RoomVO;
import com.bayihouse.service.OutPersonService;
import com.bayihouse.service.RoomService;
import com.bayihouse.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【outperson】的数据库操作Service实现
 * @createDate 2025-04-23 02:22:33
 */
@Service
public class OutPersonServiceImpl extends ServiceImpl<OutPersonMapper, OutPerson>
        implements OutPersonService {


    @Resource
    private RoomService roomService;

    @Override
    public OutPersonVO getOutPersonVO(OutPerson outperson) {
        if (outperson == null) {
            return null;
        }
        return OutPersonVO.objToVo(outperson);
    }

    @Override
    public Wrapper<OutPerson> getQueryWrapper(OutPersonQueryRequest outpersonQueryRequest) {
        Long id = outpersonQueryRequest.getId();
        String name = outpersonQueryRequest.getName();
        String telephone = outpersonQueryRequest.getTelephone();
        Integer sex = outpersonQueryRequest.getSex();
        String destination = outpersonQueryRequest.getDestination();
        Date createTime = outpersonQueryRequest.getCreateTime();
        String reason = outpersonQueryRequest.getReason();
        Long roomId = outpersonQueryRequest.getRoomId();
        long current = outpersonQueryRequest.getCurrent();
        long pageSize = outpersonQueryRequest.getPageSize();
        String sortField = outpersonQueryRequest.getSortField();
        String sortOrder = outpersonQueryRequest.getSortOrder();
        QueryWrapper<OutPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(telephone), "telephone", telephone);
        queryWrapper.eq(sex != null, "sex", sex);
        queryWrapper.like(StringUtils.isNotBlank(destination), "destination", destination);
        queryWrapper.eq(StringUtils.isNotBlank(reason), "reason", reason);
        queryWrapper.eq(createTime != null, "createTime", createTime);
        queryWrapper.eq(roomId != null, "roomId", roomId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public List<OutPersonVO> getOutPersonVOList(List<OutPerson> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getOutPersonVO).collect(Collectors.toList());
    }
}




